﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace HospitalSystem
{
    public partial class CalcuLateEvoAge : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        int analysistime;
        //Valuable For Calculate Evolution Age in Skill
        double temp1 = 0.0;
        double evolutionAge1 = 0.0;
        double countAllActivity1 = 0.0;
        double pointFormActivity1 = 0.0;
        int countGrouping1 = 0;

        double temp = 0.0;
        double evolutionAge = 0.0;
        double countAllActivity = 0.0;
        double pointFormActivity = 0.0;
        int countGrouping = 0;

        int comparetoBreakLoop = 1;


        private List<Activity> GetTypeValue()
        {
            return (from a in db.Activities
                    where a.SVCCode.Equals("A0601001") 
                    select a).ToList();
        }

        private List<SubActivity> GetMainActValue()
        {
            return (from a in db.SubActivities
                    where a.ACTCode.Equals(DropDownList2.SelectedValue) 
                    select a).ToList();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            if (!Page.IsPostBack)
            {
                DropDownList2.DataSource = GetTypeValue();
                DropDownList2.DataTextField = "ACTDesc";
                DropDownList2.DataValueField = "ACTCode";
                DropDownList2.DataBind();
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel2.Visible = true;
            DropDownList3.DataSource = GetMainActValue();
            DropDownList3.DataTextField = "SACTCode";
            DropDownList3.DataValueField = "SACTDesc";
            DropDownList3.DataBind();
            //var query2 = from a in db.SubActivities
            //             group a by a.AgeCode into g
            //             select g.Key;
            var query2 = from a in db.AgeTypes
                        orderby a.MinAgeRange
                        select a.AgeCode;

            foreach (var q2 in query2)
            {
                //Calculating Evolution Age
                var query = from r2 in
                                (from r1 in
                                     (from a in db.SubActivities
                                      join b in db.SubActivity2s on a.SACTCode equals b.SACTCode
                                      where a.ACTCode.Equals(DropDownList2.SelectedValue)
                                      select new
                                      {
                                          b.SACT2Code,
                                          b.AgeCode
                                      })
                                 join c in db.GrowthEvaluation2s on r1.SACT2Code equals c.SACT2Code
                                 where c.CaseNo.Equals(Label4.Text) && c.CID.Equals(TextBox1.Text)
                                 select new
                                 {
                                     r1.SACT2Code,
                                     r1.AgeCode,
                                     c.SACTValue
                                 })
                            join d in db.AgeTypes on r2.AgeCode equals d.AgeCode
                            select new
                            {
                                r2.SACTValue
                            };
                foreach (var q in query)
                {
                    //ทำได้
                    if (q.SACTValue == "2")
                    {
                        countAllActivity1++;
                        pointFormActivity1++;
                    }
                    //ได้แต่ต้องช่วยเหลือ
                    else if (q.SACTValue == "1")
                    {
                        countAllActivity1++;
                        pointFormActivity1 = pointFormActivity1 + 0.5;
                    }
                    //ทำไม่ได้
                    else if (q.SACTValue == "0")
                    {
                        countAllActivity1++;
                    }
                    //กรณีที่เป็น Null หรือเลือกเป็นกิจกรรมที่ต้องช่วยเหลือ
                    else
                    {
                        ;
                    }
                }

                if (countAllActivity1 != 0.0)
                {
                    //Query For Find Min and Max Age In Iteration
                    var query3 = from a in db.AgeTypes
                                 where a.AgeCode.Equals(q2)
                                 select new
                                 {
                                     a.MinAgeRange,
                                     a.MaxAgeRange
                                 };
                    foreach (var q3 in query3)
                    {
                        countGrouping1++;
                        if (countGrouping1 == 1)
                        {
                            //Evaluate Evolution Age in this Iteration
                            temp1 = ((pointFormActivity1 / countAllActivity1) * ((Convert.ToDouble(q3.MaxAgeRange) - Convert.ToDouble(q3.MinAgeRange)) * 12)) + (12 * Convert.ToDouble(q3.MinAgeRange));
                        }
                        else
                        {
                            temp1 = (pointFormActivity1 / countAllActivity1) * ((Convert.ToDouble(q3.MaxAgeRange) - Convert.ToDouble(q3.MinAgeRange)) * 12);
                        }
                        //Add Value in Evolution Age
                        evolutionAge1 = evolutionAge1 + temp1;
                        //Reset Value
                        temp1 = 0.0;
                        pointFormActivity1 = 0.0;
                        countAllActivity1 = 0.0;
                    }
                }
                //กรณีที่ส่วนเป็น 0 (countAllActivity is zero)
                else
                {
                    //Reset Value
                    temp1 = 0.0;
                    pointFormActivity1 = 0.0;
                    countAllActivity1 = 0.0;
                }

            }
            //CalculatemonthToYear(evolutionAge);
            Label5.Text = CalculatetotalmonthToYear(evolutionAge1).ToString();
            Label6.Text = CalculatetotalmonthTomonth(evolutionAge1).ToString();
            countGrouping1 = 0;
        }

        private double CalculatetotalmonthToYear(double monthAge)
        {
            return Math.Floor(monthAge / 12);
        }

        private double CalculatetotalmonthTomonth(double monthAge)
        {
            return Math.Floor(monthAge % 12);
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel2.Visible = true;
            var query = from a in db.AgeTypes
                         orderby a.MinAgeRange
                         select a.AgeCode;
            //Query by Grouping(By Agecode)
            foreach (var q in query)
            {
                var query2 = from r1 in
                                 (from a in db.GrowthEvaluation2s
                                  join b in db.SubActivity2s on a.SACT2Code equals b.SACT2Code
                                  //Assume Label1 is CaseNumber
                                  where a.CID.Equals(TextBox1.Text) && a.CaseNo.Equals(Label4.Text) && b.SACTCode.Equals(DropDownList3.SelectedValue) && b.AgeCode.Equals(q) 
                                  select new
                                  {
                                      a.SACTValue,
                                      b.SACT2Code,
                                      b.AgeCode
                                  })
                             join c in db.AgeTypes on r1.AgeCode equals c.AgeCode
                             select new
                             {
                                 r1.SACTValue
                             };
                foreach (var q2 in query2)
                {
                    //ทำได้
                    if (q2.SACTValue == "2")
                    {
                        countAllActivity++;
                        pointFormActivity++;
                    }
                    //ได้แต่ต้องช่วยเหลือ
                    else if (q2.SACTValue == "1")
                    {
                        countAllActivity++;
                        pointFormActivity = pointFormActivity + 0.5;
                    }
                    //ทำไม่ได้
                    else if (q2.SACTValue == "0")
                    {
                        countAllActivity++;
                    }
                    //กรณีที่เป็น Null หรือเลือกเป็นกิจกรรมที่ต้องช่วยเหลือ
                    else
                    {
                        ;
                    }

                }
                //Calculate Evolution Age of 1 Iteration
                if (countAllActivity != 0.0)
                {
                    //Query For Find Min and Max Age In Iteration
                    var query3 = from a in db.AgeTypes
                                 where a.AgeCode.Equals(q)
                                 select new
                                 {
                                     a.MinAgeRange,
                                     a.MaxAgeRange
                                 };
                    foreach (var q3 in query3)
                    {
                        //Add CountGrouping
                        countGrouping++;
                        //In First Iteration
                        if (countGrouping == 1)
                        {
                            //Evaluate Evolution Age in this Iteration
                            temp = ((pointFormActivity / countAllActivity) * ((Convert.ToDouble(q3.MaxAgeRange) - Convert.ToDouble(q3.MinAgeRange)) * 12)) + (12 * Convert.ToDouble(q3.MinAgeRange));

                        }
                        else
                        {
                            temp = (pointFormActivity / countAllActivity) * ((Convert.ToDouble(q3.MaxAgeRange) - Convert.ToDouble(q3.MinAgeRange)) * 12);
                        }
                        //Add Value in Evolution Age
                        evolutionAge = evolutionAge + temp;
                        //Reset Value
                        temp = 0.0;
                        pointFormActivity = 0.0;
                        countAllActivity = 0.0;
                    }
                }
                //กรณีที่ส่วนเป็น 0 (countAllActivity is zero)
                else
                {
                    //Reset Value
                    temp = 0.0;
                    pointFormActivity = 0.0;
                    countAllActivity = 0.0;
                }

            }
            //CalculatemonthToYear(evolutionAge);
            Label7.Text = CalculatetotalmonthToYear(evolutionAge).ToString();
            Label8.Text = CalculatetotalmonthTomonth(evolutionAge).ToString();
            countGrouping = 0;
        }

        private void ResetValue()
        {
            DropDownList1.Items.Clear();
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            ResetValue();
            var query = from a in db.Evaluations
                        join b in db.Persons on a.CID equals b.CID
                        where a.CID.Equals(TextBox1.Text)
                        select new
                        {
                            a.CaseNo,
                            b.FirstName,
                            b.LastName,
                            //b.DOB,
                            a.EvalDateTime
                        };
            if (query.FirstOrDefault() == null)
            {
                Label9.ForeColor = Color.Red;
                Label9.Text = "ไม่มีข้อมูล";
                //Literal1.Text = "<p style='color:Red;'>ไม่พบข้อมูลเด็ก</p>";
                //UpdatePanel1.Visible = false;
            }
            else
            {
                DropDownList1.Items.Add(new ListItem("===เลือกการประเมิน===", "Zero"));
                Label9.ForeColor = Color.Green;
                Label9.Text = "OK";
                Panel1.Visible = true;
                foreach (var q in query)
                {
                    analysistime++;
                    Label1.Text = q.FirstName;
                    Label2.Text = q.LastName;
                    //int age = DateTime.Now.Year - q.DateofBirth.Year;
                    //if (DateTime.Now < q.DateofBirth.AddYears(age))
                    //{
                    //    age--;
                    //}
                    //Label3.Text = age.ToString();
                    DropDownList1.Items.Add(analysistime.ToString());
                }
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            if (DropDownList1.SelectedValue == "Zero")
            {
                ;
            }
            else
            {
                Panel2.Visible = true;
                //comparetoBreakLoop++;
                int timeChoosing = Convert.ToInt32(DropDownList1.SelectedValue);
                var query = from a in db.Evaluations
                            where TextBox1.Text.Equals(a.CID)
                            orderby a.CaseNo
                            select a.CaseNo;
                foreach (var q in query)
                {
                    if (comparetoBreakLoop == timeChoosing)
                    {
                        Label4.Text = q.ToString();
                        break;
                    }
                    else
                    {
                        comparetoBreakLoop++;
                    }
                }

            }
        }
    }
}